package com.css.fxfzfxqh.modules.reportTemplate.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.calculationTasks.entity.CalculationTasks;
import com.css.fxfzfxqh.modules.reportTemplate.entity.Template;
import com.css.fxfzfxqh.modules.reportTemplate.service.TemplateService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年10月24日  10:29
 */
@RestController
@RequestMapping("/template")
public class ReportTemplateController {

    @Resource
    TemplateService templateService;

    /**
     * 新增报告模版
     */
    @PostMapping("/save")
    public RestResponse save(@Valid @RequestBody Template template){
        templateService.save(template);
        return ResponseCreator.createSuccessResponse("新建完成");
    }

    @GetMapping("/getByTemplateNum")
    public RestResponse getByTemplateNum(@RequestParam String num){
        int count = templateService.getByTemplateNum(num);
        if(count==0){
            return RestResponse.succeed();
        }
        return RestResponse.succeed("已存在相同类型的模板!");
    }
    @GetMapping("/getByTemplateName")
    public RestResponse getByTemplateName(@RequestParam(required = false) String name,@RequestParam(required = false) String num){
        int count = templateService.getByTemplateName(name,num);
        if(count==0){
            return RestResponse.succeed();
        }
        return RestResponse.succeed("已存在相同的模板名称!");
    }
    /**
     * 上传报告模版
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody Map<String,String> map){
        templateService.update(map.get("id"),map.get("attachId"),map.get("name"),map.get("templateDesc"));
        return ResponseCreator.createSuccessResponse("上传完成");
    }

    /**
     * 删除报告模版
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Map<String,String> map){
        templateService.delete(map.get("id"));
        return ResponseCreator.createSuccessResponse("删除完成");
    }

    /**
     * 下载报告
     */
//    @GetMapping("/download")
//    public void download(String id, HttpServletResponse res){
//        templateService.download(id,res);
//    }
    @PostMapping("/downloadReportTemplate")
    public void downloadReportTemplate(@RequestBody Map<String,String> map, HttpServletResponse res){
        templateService.download(map.get("id"),res);
    }

    /**
     * 获取模板编号
     */
    @GetMapping("/getTemplateNum")
    public RestResponse getTemplateNum(@RequestParam String reportType,@RequestParam String input){
        return templateService.getTemplateNum(reportType,input);
    }

    /**
     * 分页列表
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request,@RequestParam(required = false) String num,@RequestParam(required = false) String name) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = templateService.getPage(curPage, pageSize, num,name);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/findById")
    public RestResponse findById(@RequestParam String id){
        return RestResponse.succeed(templateService.findById(id));
    }
}
